package abc.datasource;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.Advisor;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role;

@Slf4j
@Configuration
@ConditionalOnProperty(prefix = "seata", name = "enableAutoDataSourceProxy", havingValue = "false", matchIfMissing = true)
public class DruidAutoConfiguration {
    @Resource
    IDynamicDataSourceProvider dynamicDataSourceProvider;

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DynamicDataSource dynamicDataSource() {
        log.info("-----------------DruidAutoConfiguration Enabled-----------------");
        return new DynamicDataSource(dynamicDataSourceProvider);
    }


//    @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
//    @Bean
//    @ConditionalOnProperty(prefix = "spring.datasource.dynamic", name = "seata", havingValue = "false", matchIfMissing = true)
//    public Advisor dynamicTransactionAdvisor() {
//        DynamicTransactionInterceptor interceptor = new DynamicTransactionInterceptor();
//        return new DynamicDataSourceAnnotationAdvisor(interceptor, GlobalTransactional.class);
//    }
}
